package antLogic;

import java.util.HashMap;
import java.util.Vector;

/**
 * Created by IntelliJ IDEA.
 * User: Mark the intern
 * Date: 15-Jul-2007
 * Time: 1:23:41 PM
 */
public class Map {

    public HashMap<Coordinate, Vector<PhysicalObject>> mapData;
    Integer width;
    Integer height;

    public Map(int width, int height)  {
        this.width = width;
        this.height = height;
        mapData = new HashMap<Coordinate, Vector<PhysicalObject>>();
    }

    public PhysicalObject[] getLocation(Coordinate location)  {
        PhysicalObject[] objectsAtLocation = null;
        if(mapData.containsKey(location)) {
            objectsAtLocation = mapData.get(location).toArray(new PhysicalObject[mapData.get(location).size()]);
        }
        return objectsAtLocation;
    }

    public int addItem(PhysicalObject item, Coordinate location)  {
        int numItemsAtLocation = -1;
        if(mapData.containsKey(location)) {
            mapData.get(location).add(item);
            numItemsAtLocation = mapData.get(location).size();
        } else {
            Vector objectsAtLocation = new Vector<PhysicalObject>();
            objectsAtLocation.add(item);
            mapData.put(location,objectsAtLocation);
            numItemsAtLocation = 1;
        }

        return numItemsAtLocation;
    }

    public boolean rmItem(int itemId, Coordinate location) {
        boolean foundItem = false;
        if(mapData.containsKey(location)) {
            int i = 0;
            for(PhysicalObject objectAtLocation : mapData.get(location)) {
                if(objectAtLocation.getObjectId() == itemId) {
                    mapData.get(location).remove(i);
                    foundItem = true;
                    break;
                }
                i++;
            }
        }
        return foundItem;
    }

    public int mvItem(PhysicalObject item, Coordinate source, Coordinate destination) {
        int numCollidedItem = -1;
        if(rmItem(item.getObjectId(),source)) {
            numCollidedItem = addItem(item, destination);
        }
        return numCollidedItem;
    }
}
